"""
迭代器入门示例
把一个类作为一个迭代器使用需要在类中实现两个方法 __iter__() 与 __next__() 。
"""


# 迭代器类
class MyNumbers:
    def __init__(self, n):
        self.number = n

    def __iter__(self):
        self.a = 1
        return self

    def __next__(self):
        if self.a < self.number:
            x = self.a
            self.a += 1
            return x
        else:
            raise StopIteration


# 迭代器调用
def iterator_invoke():
    myclass = MyNumbers(5)
    myiter = iter(myclass)
    print(next(myiter))
    print(next(myiter))

    for value in myiter:
        print(value)


# 程序入口
if __name__ == "__main__":
    iterator_invoke()
    pass